package com.time.workshop.ui;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.time.workshop.API;
import com.time.workshop.BaseConstant;
import com.time.workshop.R;
import com.time.workshop.SuperActivity;
import com.time.workshop.bean.BaseResponse;
import com.time.workshop.bean.UserInfo;
import com.time.workshop.injector.Injector;
import com.time.workshop.injector.V;
import com.time.workshop.utils.CommonUtils;
import com.time.workshop.utils.NewToast;
import com.time.workshop.utils.PreferenceUtil;

/**
 * 登入
 * 
 * @author wangfeng
 *
 */
public class LoginActivity extends SuperActivity implements OnClickListener
{
	@V
	private EditText et_login_phone, et_login_password;
	@V
	private TextView tv_login_rsg, forget;
	@V
	private ImageView img_login_back;
	@V
	private Button btn_login;
	private String phone, psw;

	/** 返回数据总体的实体类 */
	private class Response extends BaseResponse<UserInfo>
	{
	};

	private Response mUserInfo;
	public static LoginActivity loginActivity;

	@Override
	protected void onCreate(Bundle bundle)
	{
		super.onCreate(bundle);
		setContentView(R.layout.activity_login);
		loginActivity = this;
		init();
	}

	private void init()
	{
		Injector.getInstance().inject(this);
		btn_login.setOnClickListener(this);
		tv_login_rsg.setOnClickListener(this);
		forget.setOnClickListener(this);
		img_login_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View view)
	{
		Intent in;
		switch (view.getId())
		{
		case R.id.btn_login:

			doJudge();

			break;
		case R.id.tv_login_rsg:
			in = new Intent(LoginActivity.this, RegisterActivity.class);
			startActivity(in, true);
			break;
		case R.id.forget:
			in = new Intent(LoginActivity.this, ForgetActivity.class);
			startActivity(in, true);
			break;
		case R.id.img_login_back:
			back();
			break;

		default:
			break;
		}
		super.onClick(view);
	}

	private void doJudge()
	{

		phone = et_login_phone.getText().toString().trim();
		psw = et_login_password.getText().toString().trim();
		if (invalidateText(et_login_phone, R.string.hint_phone) || invalidateText(et_login_password, R.string.hint_password))
		{
			return;
		}

		if (!CommonUtils.isMobile(phone))
		{
			NewToast.makeText(this, getResources().getString(R.string.toast_phone));
			return;
		}
		if (!CommonUtils.isNetworkConnected(getApplicationContext()))
		{
			NewToast.makeText(getApplicationContext(), getResources().getString(R.string.M000));
			return;
		}
		// 请求登录
		doLoginRequest(phone, psw);

	}

	private void doLoginRequest(String pho, final String pwd)
	{
		showDialog(null);
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("username", pho);
		params.addQueryStringParameter("password", pwd);

		HttpUtils httpUtils = new HttpUtils();

		httpUtils.send(HttpMethod.GET, API.login, params, new RequestCallBack<String>()
		{

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo)
			{
				Log.d("onSuccess==", responseInfo.result + "");

				dismissLoadingDialog();
				try
				{
					JSONObject jsonObject = new JSONObject(responseInfo.result);
					Gson gson = new Gson();

					mUserInfo = gson.fromJson(jsonObject.toString(), Response.class);

					Log.d("mUserInfo", mUserInfo.getResult() + "");

					if (mUserInfo.isSuccess())
					{

						// 保存用户名和登录返回的Token
						PreferenceUtil.putString(LoginActivity.this, BaseConstant.PREF_USERNAME_STRING, mUserInfo.getResult().getUSERNAME());
						PreferenceUtil.putString(LoginActivity.this, BaseConstant.PREF_TOKE_STRING, mUserInfo.getResult().getTOKEN() + "");
						PreferenceUtil.putString(LoginActivity.this, BaseConstant.PREF_PASSWORD_STRING, pwd + "");
						if (mUserInfo.getResult().getHEADER_IMAGE() != null && !"".equals(mUserInfo.getResult().getHEADER_IMAGE()))
						{
							PreferenceUtil.putString(LoginActivity.this, BaseConstant.PREF_USER_FACE_IMAGE, mUserInfo.getResult().getHEADER_IMAGE());

						} else
						{
							PreferenceUtil.putString(LoginActivity.this, BaseConstant.PREF_USER_FACE_IMAGE, "");
						}

						NewToast.makeText(getApplicationContext(), R.string.login_success);

						back();

					} else
					{
						NewToast.makeText(getApplicationContext(), mUserInfo.getMessage());
					}
				} catch (JSONException e)
				{
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(HttpException error, String msg)
			{
				dismissLoadingDialog();
				Log.d("onFailure==", msg + "");
				NewToast.makeText(getApplicationContext(), R.string.login_failure);
			}

		});

	}

}
